package com.yuxi.config;

import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

/**
 * 线程池配置文件
 * Created by yuxi on 16/11/14.
 */
@Configuration
@Component
@PropertySource("classpath:application.properties")
public class ThreadPoolConfig  implements EnvironmentAware {
    private Environment env;
    @Override
    public void setEnvironment(Environment environment) {
        this.env=environment;
    }



    @Bean
    public ThreadPoolTaskExecutor getThreadPool() {
        ThreadPoolTaskExecutor threadPoolTaskExecutor =  new ThreadPoolTaskExecutor();
        threadPoolTaskExecutor.setCorePoolSize(Integer.valueOf(env.getProperty("task.core_pool_size")));
        threadPoolTaskExecutor.setMaxPoolSize(Integer.valueOf(env.getProperty("task.max_pool_size")));
        threadPoolTaskExecutor.setQueueCapacity(Integer.valueOf(env.getProperty("task.queue_capacity")));
        threadPoolTaskExecutor.setKeepAliveSeconds(Integer.valueOf(env.getProperty("task.keep_alive_seconds")));
        return threadPoolTaskExecutor;
    }
}
